//
//  LoginViewController.m
//  MyChat
//
//  Created by Ansen on 15-1-30.
//  Copyright (c) 2015年 Ansen. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
#import "NSString+Helper.h"
#import "UIImage+Fit.h"
#import "LoginUser.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *userNameTf;
@property (weak, nonatomic) IBOutlet UITextField *passwordTf;
@property (weak, nonatomic) IBOutlet UITextField *hostNameTf;

@property (weak, nonatomic) IBOutlet UIButton *registerBTn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LoginViewController
#pragma mark - AppDelegate 的助手方法
- (AppDelegate *)appDelegate
{
    return [[UIApplication sharedApplication] delegate];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1. 拉伸按钮背景图片
    // 1) 登录按钮
    [_loginBtn setBackgroundImage:[UIImage resizeImage:@"LoginGreenBigBtn"] forState:UIControlStateNormal];
    
    // 2) 注册按钮
    [_registerBTn setBackgroundImage:[UIImage resizeImage:@"LoginwhiteBtn"] forState:UIControlStateNormal];
    
    // 2. 设置界面文本的初始值
    _userNameTf.text = Login_User.userName;
    _passwordTf.text = Login_User.password;
    _hostNameTf.text = Login_User.hostName;
    
    _hostNameTf.text = @"ansendemacbook-pro.local";
    
    // 3. 设置文本焦点
    if ([_userNameTf.text isEmptyString]) {
        [_userNameTf becomeFirstResponder];
    }
}

#pragma mark UITextField代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _userNameTf) {
        [_passwordTf becomeFirstResponder];
    }else if (textField == _passwordTf  && [_hostNameTf.text isEmptyString]){
        [_hostNameTf becomeFirstResponder];
    }else{
        [self userLoginAndRegister:nil];
    }
    
    return YES;
    
}

#pragma mark - Actions
#pragma mark 用户登录
- (IBAction)userLoginAndRegister:(UIButton *)sender {
    
    // 1. 检查用户输入是否完整，在商业软件中，处理用户输入时
    // 通常会截断字符串前后的空格（密码除外），从而可以最大程度地降低用户输入错误
    NSString *userName = [_userNameTf.text trimString];
    // 用些用户会使用空格做密码，因此密码不能去除空白字符
    NSString *password = _passwordTf.text;
    NSString *hostName = [_hostNameTf.text trimString];
    
    if ([userName isEmptyString] ||
        [password isEmptyString] ||
        [hostName isEmptyString]) {
        
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登录信息不完整" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alter show];
        
        return;
    }
    
    Login_User.userName = userName;
    Login_User.password = password;
    Login_User.hostName = hostName;
    
    // 3. 让AppDelegate开始连接
    // 告诉AppDelegate，当前是注册用户
    NSString *errorMessage = nil;
    
    if (sender.tag == 1) {
        [self appDelegate].isRegisterUser = YES;
        errorMessage = @"注册用户失败";
    }else{
        errorMessage = @"用户登录失败";
    }
    
    [APP_DELEGATE connectWithCompletion:^{
        NSLog(@"成功");
    } falied:^{
        
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:errorMessage delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alter show];

        if (sender.tag == 1) {
            // 注册用户失败通常是因为用户名重复
            [_userNameTf becomeFirstResponder];
        }else{
            // 登录失败通常是密码输入错误
            [_passwordTf setText:@""];
            [_passwordTf becomeFirstResponder];
        }
        
    }];
    
    
}


@end
